<?php 
namespace app\admin\controller;
class Member extends Comm{
	//管理员列表
	public function member_list(){
		$controller = request()->controller();
		$action = request()->action();
		$controller = model('Role')->where(['controller'=>$controller])->find()->rname;
		$action = model('Role')->where(['action'=>$action])->find()->rname;

		//改变区域显示
		foreach(model('Village')->select() as $val){
			$data_area[$val->id] = $val->name;
		}
		$this->assign('data_area',$data_area);

		if(request()->ispost() && $_POST['cond'] != 'all'){
			$key = input('post.key');
			if(input('post.cond') == 'gid'){
				foreach(model('Group')->select() as $val){
					$tmp[] = $val->gname;
				}
				array_unshift($tmp,'哈罗');
				$key = array_search($key,$tmp);
			}
			if(input('post.cond') == 'area'){
				$key = array_search($key,$data_area);
			}
			$where = [
				input('post.cond')=>$key
			];
			$res = model('Member')->where($where)->select();
			if($res == []){
				$this->assign('data',$res);
				$this->assign('controller',$controller);
				$this->assign('action',$action);
				return $this->fetch();
			}
		}else{
			$res = model('Member')->select();
		}
		
		foreach($res as $v){
			$v = $v->toArray();
			$v['area'] = $data_area[$v['area']];
			$data[] = $v;
		}
		$this->assign('controller',$controller);
		$this->assign('action',$action);
		$this->assign('data',$data);
		return $this->fetch();
	}


	//添加管理员
	public function member_add(){
		foreach(model('Village')->where(['pid'=>'1'])->select() as $val){
			$data_area[] = $val->toArray();
		}
		// dump($data_area);
		$this->assign('data_area',$data_area);

		$group = model('Group')->select();
		foreach($group as $v){
			$data_group[] = $v;
		}
		$this->assign('group',$data_group);
		return $this->fetch();
	}

	//处理添加
	public function do_add(){
		$re = $this->validate($_POST,'Member');
		if($re !== true){
			$this->error($re);
		}
		$files = request()->file('thumb');
		$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'member';
		foreach($files as $file){
			$info = $file->move($path);
			if($info){
				$savename[] = $info->getSavename();
			}else{
				echo $file->getError();
			}
		}
		$_POST['thumb'] = $savename[0];
		$_POST['utime'] = date('Y-m-d H:i:s',time());
		$res = model('Member')->allowfield(true)->save($_POST);
		if($res > 0){
			$this->success('成功');
		}
	}

	//编辑管理员
	public function member_edit(){
		//改编区域显示
		foreach(model('Village')->where(['pid'=>'1'])->select() as $val){
			$data_area[] = $val->toArray();
		}
		$this->assign('data_area',$data_area);

		if(request()->isget()){
			$uid = $_GET['uid'];
			$list = model('Member')->where(['uid'=>$uid])->select();
			foreach($list as $vo){
				$data = $vo;
			}
			$group = model('Group')->select();
			foreach($group as $v){
				$data_group[] = $v;
			}
			$this->assign('group',$data_group);
			$this->assign('uid',$uid);
			$this->assign('data',$data);
			return $this->fetch();
		}
	}

	//处理编辑
	public function do_edit(){
		
		$re = $this->validate($_POST,'Member');
		if($re !== true){
			$this->error($re);
		}

		$files = request()->file('thumb');
		if($files != null){
			$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'member';
			foreach($files as $file){
				$info = $file->move($path);
				if($info){
					$savename[] = $info->getSavename();
				}else{
					echo $file->getError();
				}
			}
			$res = model('Member')->where(['uid'=>$_POST['uid']])->select();
			foreach($res as $re){
				$data[] = $re['thumb'];
				$imgs = explode(',',$re['thumb']);
				foreach($imgs as $img){
					$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'member'.DS.$img;
					unlink($path);
				}	
			}
			$_POST['thumb'] = $savename[0];
		}else{
			$_POST['thumb'] = model('Member')->where(['uid'=>$_POST['uid']])->find()->thumb;
		}

		
		$_POST['utime'] = date('Y-m-d H:i:s',time());
		$res = model('Member')->allowfield(true)->save($_POST,['uid'=>$_POST['uid']]);
		if($res > 0){
			$this->success('成功');
		}
	}

	//处理删除
	public function do_del(){
		if(request()->ispost()){
			$where = [
				'uid'=>['in',$_POST['uid']]
			];
			$res = model('Member')->where($where)->select();
			$count = model('Member')->where($where)->delete();
			foreach($res as $re){
				$data[] = $re['thumb'];
				$imgs = explode(',',$re['thumb']);
				foreach($imgs as $img){
					$path = ROOT_PATH.'public'.DS.'static'.DS.'upload'.DS.'member'.DS.$img;
					unlink($path);
				}	
			}
			if($count > 0){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}

	//管理员分组
	public function member_group(){
		//面包屑标签显示
		$controller = request()->controller();
		$action = request()->action();
		$controller = model('Role')->where(['controller'=>$controller])->find()->rname;
		$action = model('Role')->where(['action'=>$action])->find()->rname;
		$this->assign('controller',$controller);
		$this->assign('action',$action);

		//表数据
		foreach(model("Group")->select() as $val){
			$data[] = $val->toArray();
		}
		foreach($data as $va){
			$where = [
				'gid'=>$va['gid']
			];
			$res = model('Member')->where($where)->select();
			foreach($res as $re){
				$r[$va['gid']][] = $re->toArray();
			}
		}
		$this->assign('group_member',$r);
		$this->assign('data',$data);
		
		return $this->fetch();
	}


	//管理员信息简介
	public function m_info(){
		$data = model('Member')->alias('m')->join('Village v','v.id=m.area')->where(['uid'=>$_GET['uid']])->find()->toArray();
		$count1 = model('Contract')->where(['uid'=>$_GET['uid']])->count();
		$count2 = model('Service')->where(['mid'=>$_GET['uid'],'statu'=>2])->count();
		$count = $count1+$count2;
		$this->assign('count',$count);
		$this->assign('data',$data);
		return $this->fetch();
	}

	//添加分组
	public function group_add(){
		foreach(model('Role')->where(['fid'=>0])->select() as $val){
			$rids[] = $val->toArray();
		}
		foreach($rids as $va){
			$where = [
				'fid'=>$va['rid']
			];
			$res = model('Role')->where($where)->select();
			foreach($res as $re){
				$role[$va['rname']][] =$re->toArray();
			}
		}

		$this->assign('role',$role);
		return $this->fetch();
	}

	//处理分组添加
	public function do_groupadd(){
		$rids = implode(',',$_POST['rid']);
		$where = [
			'rid'=>['in',$rids]
		];
		foreach(model('Role')->where($where)->select() as $val){
			$data[] = $val->fid;
		}
		$rids = $rids.','.implode(',',array_unique($data));
		$_POST['rid'] = $rids;
		$res = model('Group')->allowfield(true)->save($_POST);
		if($res > 0){
			$this->success('成功');
		}else{
			$this->error($rids);
		}
	}

	//处理角色删除
	public function do_groupdel(){
		if(request()->ispost()){
			$where = [
				'gid'=>['in',$_POST['gid']]
			];
			$count = model('Group')->where($where)->delete();
			
			if($count > 0){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
	}

	//角色编辑
	public function group_edit(){
		foreach(model('Role')->where(['fid'=>0])->select() as $val){
			$rids[] = $val->toArray();
		}
		foreach($rids as $va){
			$where = [
				'fid'=>$va['rid']
			];
			$res = model('Role')->where($where)->select();
			foreach($res as $re){
				$role[$va['rname']][] =$re->toArray();
			}
		}
		$this->assign('role',$role);

		if(request()->isget()){
			$gid = $_GET['gid'];
			$data = model('Group')->where(['gid'=>$gid])->find()->toArray();
			$rid2s = model('Group')->where(['gid'=>$gid])->find()->rid;
			$rid2s = explode(',',$rid2s);
			$this->assign('rid2s',$rid2s);
			$this->assign('data',$data);

		}


		return $this->fetch();
	}

	//处理角色编辑
	public function do_groupedit(){
		$rids = implode(',',$_POST['rid']);
		$where = [
			'rid'=>['in',$rids]
		];
		foreach(model('Role')->where($where)->select() as $val){
			$data[] = $val->fid;
		}
		$rids = $rids.','.implode(',',array_unique($data));
		$_POST['rid'] = $rids;
		$res = model('Group')->allowfield(true)->where(['gid'=>$_POST['gid']])->update($_POST);
		if($res > 0){
			$this->success('成功');
		}else{
			$this->error($rids);
		}
		
	}


}


 ?>